const router = require('koa-router')()
const Redis = require('koa-redis') 

const nodeMailer = require('nodemailer')
const User = require('../dbs/models/userManagement')
const Passport = require('./utils/passport')
const Email = require('../dbs/config')
const axios = require('./utils/axios')

router.prefix('/userManagement')
// let router = new Router({
//   prefix: "/users"
// })

// 获取Redis客户端
let Store = new Redis().client


router.post('/add', async (ctx, next)=>{
  // 检测是否登录状态 
  if(ctx.isAuthenticated()) { 
   
    const {
      username,
      truename,
      phone,
      job_statue,
      role
    } = ctx.request.body; //post方式
    console.log(ctx.request.body)
    /* ctx.body = {
      code: 0,
      msg: "新增用户成功！",
    } */
    // 将该用户存储到数据库中  
    let nuser = await User.create({
      username,
      truename,
      phone,
      job_statue,
      role
    })
    if(nuser) {
      // 新增用户成功
      ctx.body = {
        code: 0,
        msg: "新增用户成功！",
      }
    } else {
      ctx.body = {
        code: -1,
        msg: "新增用户失败！", 
      }
    }
  } else {
    ctx.body = {
      code: -1,
      msg: "请先登录！", 
    }
  }
})

router.post('/list', async (ctx, next)=>{
  // 检测是否登录状态 
  if(ctx.isAuthenticated()) {
    // 将该用户存储到数据库中  
   let user = await User.find({
      username
    })
    ctx.body = {
      user: username 
    }
  } else {
    ctx.body = {
      user: "",
      email: ""
    }
  }
})

module.exports = router